home *** CD-ROM | disk | FTP | other *** search
- Path: nickel.ucs.indiana.edu!gompa
- From: gompa@nickel.ucs.indiana.edu (Raghu R. Gompa)
- Newsgroups: comp.lang.c
- Subject: Simple C program to list contents of a file - help!
- Date: 28 Jan 1996 20:04:24 GMT
- Organization: Indiana University, Bloomington
- Message-ID: <4egks8$nlu@usenet.ucs.indiana.edu>
- NNTP-Posting-Host: nickel.ucs.indiana.edu
- NNTP-Posting-User: gompa
-
-
- I am trying to figure out what is wrong with this
- program. The following is written for dos environment:
- "typeme.c"
-
- It is supposed to get words from file and list them with
- arguments like help me please with the command:
- typeme -f file help me please
-
- Depending on the number of arguments, I sometimes getting
- strange error messages - system stuck some times.
- Please help. This is part of a project (not a homework)
- I am trying to complete. .. Thanks in advance. .. Raghu
-
-
- #include "stdio.h"
- #include "string.h"
-
- FILE *fopen(), *fp;
- extern exit();
- main(int argc, char *argv[])
- {
- char first[100], lett, *words[100], filename[15];
- int numwords, cont=1;
- int i, j, repeat=10, r,k=0,t;
- int nrem;
- if(*argv[1] == '-') {
- strcpy(filename, argv[2]);
- fp= fopen(filename,"r");
- if(fp == NULL) printf("Can't open %s \n",filename);
- k=0;*words[1]='1';
- while(fscanf(fp, "%s", words[k]) != EOF ) {
- k++;
- }
- fclose(fp);
- numwords=k-1;
- if(argc>2) {
- for(i=3;i<=argc;i++) {
- strcpy(words[k],argv[i]);
- k++;
- }
- }
- numwords=k-1;
- }
- else
- {
- numwords=argc-1;
- for(k=0;k<numwords;k++)
- strcpy(words[k],argv[k+1]);
- }
-
- nrem=1;
- for(i=0;i< numwords;i++) {
- printf("%3d. %10s", i+1, words[i]);
- nrem=(i%4);
- if(i>3 && !nrem) printf("\n");
- }
- }
-
-